package com.yl.system.controller;


import com.yl.system.service.MinioService;
import com.yl.system.util.MinioConfig;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/test")
public class MinioController {

    @Autowired
    MinioConfig minioConfig;

    // 上传
    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        return this.minioConfig.upload(multipartFile);
    }

    // 下载文件
    @GetMapping("/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        this.minioConfig.download(fileName, response);
    }

    // 列出所有存储桶名称
    @PostMapping("/list")
    public List<String> list() throws Exception {
        return this.minioConfig.listBucketNames();
    }

    // 创建存储桶
    @PostMapping("/createBucket")
    public boolean createBucket(String bucketName) throws Exception {
        return this.minioConfig.makeBucket(bucketName);
    }

    // 删除存储桶
    @PostMapping("/deleteBucket")
    public boolean deleteBucket(String bucketName) throws Exception {
        return this.minioConfig.removeBucket(bucketName);
    }

    // 列出存储桶中的所有对象名称
    @PostMapping("/listObjectNames")
    public List<String> listObjectNames(String bucketName) throws Exception {
        return this.minioConfig.listObjectNames(bucketName);
    }

    // 删除一个对象
    @PostMapping("/removeObject")
    public boolean removeObject(String bucketName, String objectName) throws Exception {
        return this.minioConfig.removeObject(bucketName, objectName);
    }

    // 文件访问路径
    @PostMapping("/getObjectUrl")
    public String getObjectUrl(String bucketName, String objectName) throws Exception {
        return this.minioConfig.getObjectUrl(bucketName, objectName);
    }
    @Autowired
    private MinioService minioService;

    @PostMapping("/add")
    public String add(@RequestParam("file") String multipartFile) throws Exception {
        //开始时间
        long startTime = System.currentTimeMillis(); // 获取开始时间
        System.out.println("开始时间：" + startTime);

        minioService.add(multipartFile); // 执行方法调用

        long endTime = System.currentTimeMillis(); // 获取结束时间
        System.out.println("结束时间：" + endTime);

        long duration = endTime - startTime; // 计算耗时

        System.out.println("耗时：" + duration + " 毫秒");
        System.out.println("耗时：" + duration / 1000 + " 秒");
        System.out.println("耗时：" + duration / 60000 + " 分钟");
        System.out.println("耗时：" + duration / 60000 + " 分钟");
        return  new String("添加成功");
    }


    /**
     * 添加个人信息以及作品集
     */

    @PostMapping("informationAdd")
    public String informationAdd(@RequestParam("file") String filePath){
        minioService.informationAdd(filePath);
        return "添加成功";
    }

    /**
     * 添加字库图片
     */
    @PostMapping("addWord")
    public String addWord() throws IOException, InterruptedException {
        //开始时间
        long startTime = System.currentTimeMillis(); // 获取开始时间
        System.out.println("开始时间：" + startTime);

        String filePath = "C:\\Users\\Administrator\\Desktop\\书画同源字体库";
        System.out.println("filePath:"+filePath);
        minioService.addWord(filePath);


        long endTime = System.currentTimeMillis(); // 获取结束时间
        System.out.println("结束时间：" + endTime);

        long duration = endTime - startTime; // 计算耗时

        System.out.println("耗时：" + duration + " 毫秒");
        System.out.println("耗时：" + duration / 1000 + " 秒");
        System.out.println("耗时：" + duration / 60000 + " 分钟");
        System.out.println("耗时：" + duration / 60000 + " 分钟");
        return  new String("添加成功");

    }

    /**
     * 图片 base64转换成svg
     */
    @PostMapping("base64ToSvg")
    public String base64ToSvg() throws IOException, InterruptedException {

        return minioService.base64ToSvg();
    }

    @PostMapping("getSubFolderAndFiles")
    public String getSubFolderAndFiles() throws IOException, InterruptedException {

        String a = "C:\\Users\\Administrator\\Desktop\\22\\11406bf90d6719afbf8a48a02ec4902.png";
        return minioService.getSubFolderAndFiles(a);
    }
}
